//
//  GYZFeaturedDetailViewController.m
//  Qyer
//
//  Created by GYz on 15/10/24.
//  Copyright © 2015年 GF. All rights reserved.
//

#import "GYZFeaturedDetailViewController.h"
#import "BaseHeader.h"
@interface GYZFeaturedDetailViewController ()<UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong)UIView *navView;
@property (nonatomic, strong)NSMutableArray *poisArr;
@property (nonatomic, strong)UIImageView *topImage;
@property (nonatomic, strong)UIImageView *userImage;
@property (nonatomic, strong)UILabel *userName;
@property (nonatomic, strong)UILabel *titleLabel;
@property (nonatomic, strong)UILabel *descriptionLabel;
@property (nonatomic, strong)UILabel *recommendLabel;
@property (nonatomic, assign)int page;

@end

@implementation GYZFeaturedDetailViewController
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.infoArr = [[NSMutableArray alloc] init];
    _poisArr = [[NSMutableArray alloc] init];
    
    self.navigationController.automaticallyAdjustsScrollViewInsets = NO;
    
    self.page = 1;
    [self navigation];
    [self afn:1];
    
}

-(void)navigation
{
    _navView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [_navView setBackgroundColor:[UIColor colorWithRed:.98 green:.98 blue:.95 alpha:1]];
    _navView.alpha = 0;
    
    
    UIButton *backBut = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBut setImage:[UIImage imageNamed:@"whiteback.png"] forState:UIControlStateNormal];
    backBut.frame = CGRectMake(10, 30, 24, 24);
    [backBut addTarget:self action:@selector(backViewClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:backBut];
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeCustom];
    [back setImage:[UIImage imageNamed:@"iconfont-31fanhui1"] forState:UIControlStateNormal];
    back.frame = CGRectMake(10, 30, 24, 24);
    [back addTarget:self action:@selector(backViewClick:) forControlEvents:UIControlEventTouchUpInside];
    [_navView addSubview:back];
    
    
    
    [self.view addSubview:_navView];
    
}
#pragma mark - 点击 < 回到上一页
-(void)backViewClick:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    if (self.tableView.contentOffset.y > 0 ) {
        _navView.alpha = 0.01 * self.tableView.contentOffset.y;
    }
    if (self.tableView.contentOffset.y < 0 ) {
        _topImage.frame = CGRectMake(self.tableView.contentOffset.y * 0.75, self.tableView.contentOffset.y, SCREEN_WIDTH - 2 * self.tableView.contentOffset.y * 0.75, (SCREEN_WIDTH - 2 * self.tableView.contentOffset.y * 0.75) / 2);
        _userImage.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_WIDTH / 2 + self.tableView.contentOffset.y * 0.25);
        _userName.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_WIDTH / 2 + self.tableView.contentOffset.y * 0.25 + 40);
        _titleLabel.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_WIDTH / 2 + self.tableView.contentOffset.y * 0.25 + 80);
        _descriptionLabel.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_WIDTH / 2 + self.tableView.contentOffset.y * 0.25 + 170);
        _recommendLabel.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_WIDTH / 4 + self.tableView.contentOffset.y);
    }
    
    
}

- (void)afn:(int)num
{

    [AFN getdataWithString:[NSString stringWithFormat:@"http://open.qyer.com/qyer/footprint/mguide_detail?client_id=qyer_ios&client_secret=cd254439208ab658ddf9&count=10&id=%@&oauth_token=d5ecb9febb61510d54ba2d47d986cf65&page=%d&track_app_version=6.6.6&track_user_id=6695170&v=1", self.cid, num] block:^(id block) {
        
        NSLog(@"%@", block);
        
        NSDictionary *dataDic = [block objectForKey:@"data"];
        GYZFeaturedModel *model = [GYZFeaturedModel modelWithDictionary:dataDic];
        [self.infoArr addObject:model];
        
        for (NSDictionary *dic in [dataDic objectForKey:@"pois"]) {
            GYZFeaturedDetailModel *model = [GYZFeaturedDetailModel modelWithDictionary:dic];
            [_poisArr addObject:model];
        }
        
        
        if (!self.tableView) {
            [self creatTableView];
        }
        
        [self.tableView reloadData];
        [self.tableView.footer endRefreshing];
        
    } BLOCK:^(id BLOCK) {
    }];
}

- (void)allData
{
    __block GYZFeaturedDetailViewController *detail = self;
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        detail.page += 1;
        [detail afn:detail.page];
    }];
}

- (void)creatHeaderImage
{
    GYZFeaturedModel *model = self.infoArr[0];
    CGFloat desHeight = [StringWithHeight HeightWithText:model.cDescription width:(SCREEN_WIDTH - 20) font:[UIFont systemFontOfSize:13.0]];

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 2 + 160 + desHeight)];
    
    
    _topImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 2)];
    [_topImage sd_setImageWithURL:[NSURL URLWithString:model.photo] placeholderImage:[UIImage imageNamed:@"zhanweiPic"]];
    [view addSubview:_topImage];
    
    _userImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    _userImage.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_WIDTH / 2);
    [_userImage sd_setImageWithURL:[NSURL URLWithString:model.avatar] placeholderImage:[UIImage imageNamed:@"zhanweiPic"]];
    _userImage.layer.masksToBounds = YES;
    _userImage.layer.cornerRadius = _userImage.frame.size.width / 2;
    [view addSubview:_userImage];
    
    _userName = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
    _userName.text = model.username;
    _userName.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_WIDTH / 2 + 40);
    _userName.font = [UIFont systemFontOfSize:12.0];
    _userName.textColor = [UIColor lightGrayColor];
    [_userName setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:_userName];
    
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 60, 60)];
    _titleLabel.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_WIDTH / 2 + 80);
    _titleLabel.text = model.title;
    _titleLabel.numberOfLines = 0;
    [_titleLabel setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:_titleLabel];
    
    _descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 20, desHeight)];
    _descriptionLabel.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_WIDTH / 2 + 170);
    _descriptionLabel.text = model.cDescription;
    _descriptionLabel.numberOfLines = 0;
    _descriptionLabel.font = [UIFont systemFontOfSize:13.0];
    _descriptionLabel.textColor = [UIColor lightGrayColor];
    [view addSubview:_descriptionLabel];
    
    _recommendLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH / 2, 30)];
    _recommendLabel.text = @"----目的地主题推荐----";
    _recommendLabel.textColor = [UIColor whiteColor];
    _recommendLabel.center = CGPointMake(SCREEN_WIDTH / 2, SCREEN_WIDTH / 4);
    [_recommendLabel setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:_recommendLabel];
    
    self.tableView.tableHeaderView = view;
}

- (void)creatTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 30) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = NO;
    self.tableView.pagingEnabled = NO;
    [self allData];
    [self creatHeaderImage];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _poisArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"featuredDetail";
    GYZFeaturedDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[GYZFeaturedDetailTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    GYZFeaturedDetailModel *model = _poisArr[indexPath.row];
    [cell setValueWithModel:model];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GYZFeaturedDetailModel *model = _poisArr[indexPath.row];
    CGFloat height = [StringWithHeight HeightWithText:model.cDescription width:SCREEN_WIDTH - 20 font:[UIFont systemFontOfSize:13.0]];
    return SCREEN_WIDTH / 2 + 20 + height;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
